//
//  XSSlider.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/10/10.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSSlider.h"

@interface XSSlider ()
@property (nonatomic, strong) UILabel *valueLabel;
@property (nonatomic, strong) UIView *thumbView;

@end

@implementation XSSlider

//- (CGRect)trackRectForBounds:(CGRect)bounds{
//    return CGRectMake(0, (CGRectGetHeight(self.frame)-self.trackHeight)/2, CGRectGetWidth(self.frame), self.trackHeight);
//}
//- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
//    CGFloat y = rect.origin.y + rect.size.height/2 - 6;
//    return CGRectMake(value * rect.size.width - 6, y, 12, 12);
//}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//         self.trackHeight = 2;
//        [self setThumbImage:[UIImage imageNamed:@"audio-btn_Progressbar_night"] forState:UIControlStateNormal];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
//         self.trackHeight = 2;
//        [self setThumbImage:[UIImage imageNamed:@"audio-btn_Progressbar_night"] forState:UIControlStateNormal];
    }
    return self;
}



- (UIView *)thumbView {
    if (_thumbView == nil) {
        for (UIView *view in self.subviews.reverseObjectEnumerator.allObjects) {
            if (![view isEqual:self.valueLabel]) {
                _thumbView = view;
                break;
            }
        }
    }
    return _thumbView;
}

- (void)setValue:(float)value {
    [super setValue:value];
    self.valueLabel.text = [NSString stringWithFormat:@"%d", @(value).intValue];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.valueLabel.center = CGPointMake(CGRectGetMidX(self.thumbView.frame), CGRectGetMinY(self.thumbView.frame) - 10.0);
}

- (UILabel *)valueLabel {
    if (_valueLabel == nil) {
        _valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 15)];
        _valueLabel.font = [UIFont systemFontOfSize:13.0];
        _valueLabel.textColor = [UIColor grayColor];
        _valueLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_valueLabel];
    }
    return _valueLabel;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    self.valueLabel.center = CGPointMake(CGRectGetMidX(self.thumbView.frame), CGRectGetMinY(self.thumbView.frame) - 10.0);
    self.valueLabel.text = [NSString stringWithFormat:@"%d", @(self.value).intValue];
    return [super continueTrackingWithTouch:touch withEvent:event];
}



@end
